require(['config'], function() {
    require(['jquery', 'loadFooter', 'jquery.cookie'],($)=>{

        // 密码登录和短信登录切换
        $('.form-tab').on('click', 'span', e => {
            const index = $(e.target).index()
            // console.log(index)
            $('.form-tab>span')
            .eq(index).toggleClass('xbb')
            .siblings().removeClass('xbb')
            $('form>ul>div')
            .eq(index).toggleClass('xianshi')
            .siblings().removeClass('xianshi')     
        })

        // 密码登录和扫码登录切换
        // $('.t-tab').on('click', '', e => {
       
        // })

        //表单验证
        $('form').on('blur','#username, #pwd',function(e){
            //正则
            const usernameReg = /^[a-zA-Z]\w{3,19}$/
            const passwordReg = /^.{6,32}$/
            if($(e.target).is('#username')){
                const userName = $.trim($('#username').val())
                if(userName ==='' || (userName !== '' && !usernameReg.test(userName))){
                    alert('用户名格式不符合规范')
                    return
                }
            }
            if($(e.target).is('#pwd' )){
                const passWord = $.trim($('#pwd').val())
                if( passWord === '' || (passWord !== '' && !passwordReg.test(passWord))){
                    alert('密码格式不符合规格')
                    return
                }
            }
        })

        // 账号密码登录
        $('form').on('click', 'button', e => {
            e.preventDefault()
            const username = $('#username').val()
            const password = $('#pwd').val()
            const reg = {
                username,
                password
            }
            $.cookie.json = true
            const readCookie = $.cookie('info')
            // console.log(readCookie)
            const index = readCookie.findIndex(prod => prod.username === reg.username)
            if(index !== -1){
                if(readCookie[index].password === reg.password){
                    alert('登录成功，为你跳转到主页')
                    location.href = "/index.html"
                }else{
                    alert('密码错误，请重新输入')
                }
            }else{
                alert('用户名或密码错误')
            }
        })
        

    })
})
